// components/musicList.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    listId: String,
    listType:String
  },
  /**
   * 组件的初始数据
   */
  data: {
    data: null,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    update: function (e) {
      this.triggerEvent('update')
    },
    toComment: function () {
      wx.navigateTo({
        url: `/pages/comment/comment?id=${this.properties.listId}&type=l&image=${this.data.data.image}&title=${this.data.data.title}&user=${this.data.data.creatorName}`,
      })
    },
    addToListAndPlay: function (e) {
      const index = e.currentTarget.dataset.musicid
      app.setMusicAndPlay(this.data.data.musicList[index], this.data.data.singerList[index].name);
      this.update()
    },
    addToList: function (e) {
      console.log("加入歌单");
      const index = e.currentTarget.dataset.musicid
      app.pushMusicToList(this.data.data.musicList[index], this.data.data.singerList[index].name);
      this.update()
    },
    addAllToList: function () {
      if (this.data.data != null) {
        for (let i = 0; i < this.data.data.musicList.length; i++) {
          app.pushMusicToList(this.data.data.musicList[i], this.data.data.singerList[i].name);
        }
        app.globalData.audioCtx.onCanplay(() => {
          app.globalData.audioCtx.play()
          app.globalData.state = "playing"
          this.update()
        })
      }
    },
  },
  lifetimes: {
    attached: function () {
      let that = this
      console.log(this.properties.listId);
      wx.request({
        url: `http://www.mmdccj.top:9003/music/getList?musicListId=${this.properties.listId}`,
        method: 'GET',
        success: function (res) {
          that.setData({
            data: res.data.data
          })
          let param = 'data.image'
          let image = that.data.data.image
          console.log('拿到的image',image);
          that.setData({
            [param]: "http://www.mmdccj.top:9003/static/image/" + image
          })
          console.log(that.data);
        }
      })
    }
  }
})